<?php

class equipmentController extends Controller{
    public function equipmentdata(){
        $equipmentModel = new equipmentModel();
        $equipment = $equipmentModel->selectequipment();
        $this->assign('equipments',$equipment);
        // var_dump($user);
        $this->display('equipment.tpl');
        
    }


    public function deleteequipment(){

        $equipmentid = isset($_GET['equipmentid'])?$_GET['equipmentid']:'';
        
        $equipmentModel = new equipmentModel();
        
        $deleteequipment = $equipmentModel->deleteequipment($equipmentid);
       
        if($deleteequipment){
           echo json_encode(['code'=>1]);
        }else{
            echo json_encode(['code'=>2]);
        }
   }

   public function addequipment(){
    //获取数据
    $equipment_name = $_POST['equipment_name'];
    $equipment_price = $_POST['equipment_price'];
    $equipment_property = $_POST['equipment_property'];
    //获取英雄皮肤图片
    if(isset($_FILES['file'])){
        $file = $_FILES['file'];
        $suffixarr = ['jpg','png','gif','jpeg'];
        $filename = $file['name'];
        $namearr = explode('.', $filename);
        $suffix = end($namearr);
    if((
            $file['type'] == 'image/jpeg' || 
            $file['type'] == 'image/jpg' ||
            $file['type'] == 'image/png' || 
            $file['type'] == 'image/gif') &&
            $file['size'] < 1048576 &&
            in_array($suffix, $suffixarr)
        ){
            if($file['error']>0){
                echo json_encode(['code'=>203,'msg'=>'文件异常']);
            }else{
                $newname = time().rand(1, 100).".".$suffix;
                $newpath = dirname(dirname(dirname(dirname(__FILE__))))."\\public\\img\\".$newname;
//					echo "$newpath";
                $rst = move_uploaded_file($file['tmp_name'], $newpath);
                if($rst){
                    $equipment_equipment="./public/img/".$newname;
                    $equipmentModel = new equipmentModel();
                    $addequipment = $equipmentModel->addequipment($equipment_equipment, $equipment_name, $equipment_price, $equipment_property);
                    if($addequipment){
                        echo json_encode(['code'=>1]);
                    }else{
                         echo json_encode(['code'=>2,'msg'=>'未知原因']);
                    }
                }
            }
            }else{
            echo json_encode(['code'=>201,'msg'=>'文件格式不正确']);
        }
    }
    
   
}


public function modifyequipment(){
    //获取数据
    $equipment_id = $_POST['equipment_id'];
    $modify_property = $_POST['modify_property'];
    $modify_name = $_POST['modify_name'];
    $modify_price = $_POST['modify_price'];
    //获取英雄皮肤图片
    if(isset($_FILES['file'])){
        $file = $_FILES['file'];
        $suffixarr = ['jpg','png','gif','jpeg'];
        $filename = $file['name'];
        $namearr = explode('.', $filename);
        $suffix = end($namearr);
    if((
            $file['type'] == 'image/jpeg' || 
            $file['type'] == 'image/jpg' ||
            $file['type'] == 'image/png' || 
            $file['type'] == 'image/gif') &&
            $file['size'] < 1048576 &&
            in_array($suffix, $suffixarr)
        ){
            if($file['error']>0){
                echo json_encode(['code'=>203,'msg'=>'文件异常']);
            }else{
                $newname = time().rand(1, 100).".".$suffix;
                $newpath = dirname(dirname(dirname(dirname(__FILE__))))."\\public\\img\\".$newname;
//					echo "$newpath";
                $rst = move_uploaded_file($file['tmp_name'], $newpath);
                if($rst){
                    $modify_equipment="./public/img/".$newname;
                    $equipmentModel = new equipmentModel();
                    $modifyequipment = $equipmentModel->modifyequipmentimg($modify_equipment,$modify_name,$modify_price,$modify_property,$equipment_id);
                    if($modifyequipment){
                        echo json_encode(['code'=>1]);
                    }else{
                         echo json_encode(['code'=>2,'msg'=>'未知原因']);
                    }
                }
            }
            }else{
            echo json_encode(['code'=>201,'msg'=>'文件格式不正确']);
        }
    }else{
        //没有皮肤图片传入
        $equipmentModel = new equipmentModel();
        
        $modifyequipment = $equipmentModel->modifyequipment($modify_name,$modify_price,$modify_property,$equipment_id);
        
        if($modifyequipment){
            echo json_encode(['code'=>1]);
        }else{
            echo json_encode(['code'=>2,'msg'=>'未知原因']);
        }
    }
    
   
}


public function searchequipment(){
    $equipment_text =$this->post("equipment_text");
    $equipmentModel = new equipmentModel();
        
    $equipment = $equipmentModel->searchequipment($equipment_text);
    $this->assign('equipments',$equipment);
        // var_dump($user);
    $this->display('equipment.tpl');
    
}



}